package com.sch.wanandroid.ui.main.discover

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.sch.wanandroid.R
import com.sch.wanandroid.adapter.DiscoverFragmentAdapter
import com.sch.wanandroid.base.BaseFragment
import com.sch.wanandroid.ui.main.discover.navigation.NavigationFragment
import com.sch.wanandroid.ui.main.discover.system.SystemFragment
import com.sch.wanandroid.ui.main.home.HomeFragment
import com.sch.wanandroid.ui.main.tab.list.TabListFragment
import kotlinx.android.synthetic.main.fragment_discover.*
import kotlinx.android.synthetic.main.fragment_discover.tabLayout
import kotlinx.android.synthetic.main.fragment_discover.viewPager
import kotlinx.android.synthetic.main.fragment_refresh_list.*
import kotlinx.android.synthetic.main.fragment_tab.*

/**
 * Created by Sch.
 * Date: 2020/4/22
 * description:发现页
 */
class DiscoverFragment : BaseFragment() {
    companion object {
        fun getInstance(): DiscoverFragment = DiscoverFragment()
    }

    private val fragmentList by lazy { ArrayList<Fragment>() }
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_discover, null)
    }

    override fun init(savedInstanceState: Bundle?) {
        fragmentList.add(SystemFragment())
        fragmentList.add(NavigationFragment())
        viewPager.adapter = DiscoverFragmentAdapter(fragmentList, childFragmentManager)
        viewPager.offscreenPageLimit = 2
        tabLayout.setupWithViewPager(viewPager)
    }

    fun scrollToTop() {
        if (fragmentList.size > 0) {
            if (viewPager.currentItem == 0) {
                val fragment: SystemFragment =
                    (viewPager.adapter as DiscoverFragmentAdapter).getItem(0) as SystemFragment
                fragment.srcollToTop()
            } else if (viewPager.currentItem == 1) {
                val fragment: NavigationFragment =
                    (viewPager.adapter as DiscoverFragmentAdapter).getItem(1) as NavigationFragment
                fragment.srcollToTop()
            }
        }

    }
}